﻿using WeDonekRpc.Helper;

namespace Basic.UserFormService.PageControl.Calculate.Extend
{
    public enum TimeFormat
    {
        月初 = 0,
        月末 = 1,
        年初 = 2,
        年末 = 3
    }
    public static class TimeExtend
    {
        /// <summary>
        /// 天
        /// </summary>
        private const string _Day = "d";
        /// <summary>
        /// 月
        /// </summary>
        private const string _Month = "m";
        /// <summary>
        /// 年
        /// </summary>
        private const string _Year = "y";

        /// <summary>
        /// 小时
        /// </summary>
        private const string _Hour = "h";

        /// <summary>
        /// 分
        /// </summary>
        private const string _Min = "min";

        /// <summary>
        /// 秒
        /// </summary>
        private const string _Sec = "s";

        /// <summary>
        /// 格式化时间
        /// </summary>
        /// <param name="time"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string Format ( DateTime time, string format )
        {
            if ( format.IsNull() )
            {
                return time.ToString("yyyy-MM-dd HH:mm:ss");
            }
            return time.ToString(format);
        }
        /// <summary>
        /// 转换为时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetTimeSpan ( DateTime? time )
        {
            if ( time.HasValue )
            {
                return time.Value.ToLong();
            }
            return Tools.GetTimeSpan();
        }

        /// <summary>
        /// 格式化时间
        /// </summary>
        /// <param name="time"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static DateTime FormatTime ( DateTime time, string format )
        {
            if ( format.IsNull() )
            {
                return time;
            }
            return DateTime.Parse(time.ToString(format));
        }
        /// <summary>
        /// 获取指定格式的时间
        /// </summary>
        /// <param name="time"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DateTime GetTime ( DateTime time, TimeFormat type )
        {
            switch ( type )
            {
                case TimeFormat.月初:
                    return new DateTime(time.Year, time.Month, 1);
                case TimeFormat.月末:
                    return new DateTime(time.Year, time.Month, 1).AddMonths(1).AddDays(-1);
                case TimeFormat.年初:
                    return new DateTime(time.Year, 1, 1).AddMonths(1).AddDays(-1);
                case TimeFormat.年末:
                    return new DateTime(time.Year, 1, 1).AddYears(1).AddDays(-1);
                default:
                    return time;
            }
        }
        /// <summary>
        /// 获取当月月初
        /// </summary>
        /// <returns></returns>
        public static DateTime MonthStart ()
        {
            DateTime now = DateTime.Now.Date;
            return new DateTime(now.Year, now.Month, 1);
        }
        /// <summary>
        /// 获取当月月末
        /// </summary>
        /// <returns></returns>
        public static DateTime MonthEnd ()
        {
            DateTime date = MonthStart();
            return date.AddMonths(1).AddDays(-1);
        }
        /// <summary>
        /// 获取当年年初
        /// </summary>
        /// <returns></returns>
        public static DateTime YearStart ()
        {
            return new DateTime(DateTime.Now.Year, 1, 1);
        }
        /// <summary>
        /// 获取当年年末
        /// </summary>
        /// <returns></returns>
        public static DateTime YearEnd ()
        {
            DateTime date = YearStart();
            return date.AddYears(1).AddDays(-1);
        }

        public static DateTime Add ( DateTime time, string type, int num )
        {
            switch ( type )
            {
                case _Day:
                    return time.AddDays(num);
                case _Month:
                    return time.AddMonths(num);
                case _Year:
                    return time.AddYears(num);
                case _Hour:
                    return time.AddHours(num);
                case _Min:
                    return time.AddMinutes(num);
                case _Sec:
                    return time.AddSeconds(num);
                default:
                    return time;
            }
        }
        /// <summary>
        /// 获取当前时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetDateTime ()
        {
            return DateTime.Now;
        }
        /// <summary>
        /// 获取当前日期
        /// </summary>
        /// <returns></returns>
        public static DateTime GetDate ()
        {
            return DateTime.Now.Date;
        }
        /// <summary>
        /// 获取年份
        /// </summary>
        /// <returns></returns>
        public static int GetYear ()
        {
            return DateTime.Now.Year;
        }
        /// <summary>
        /// 获取月份
        /// </summary>
        /// <returns></returns>
        public static int GetMonth ()
        {
            return DateTime.Now.Month;
        }
        /// <summary>
        /// 获取日期
        /// </summary>
        /// <returns></returns>
        public static int GetDay ()
        {
            return DateTime.Now.Day;
        }
        /// <summary>
        /// 获取日期
        /// </summary>
        /// <returns></returns>
        public static int GetHour ()
        {
            return DateTime.Now.Hour;
        }
        /// <summary>
        /// 获取日期
        /// </summary>
        /// <returns></returns>
        public static int GetMinute ()
        {
            return DateTime.Now.Minute;
        }
    }
}
